/**
 * 系统日志
 */
Ext.define('Module.System.LogUser', {
	extend : 'Ext.panel.Panel',
	layout: 'fit',
	initComponent : function() {
		var me = this;
		var store=Ext.create('Ext.data.Store',{
			autoLoad: true,
			fields: ['id','title','mark','content','crtime','cruser','name',"ipaddr","success"],
	        proxy: {
	            type: 'ajax',
	            url: 'logAction/getUserLog.do',
	            reader: {
					root: 'datas',
					totalProperty: 'total'
				}
	        }
		});
		 //设置复选框
        var sm = new Ext.selection.CheckboxModel({
			checkOnly : false,
			enableKeyNav:true
		});
		
		var logSysPanel = me.logSysPanel= Ext.create('Ext.grid.Panel',{
//			title: '用户日志列表',
//			region: 'center',
//			padding: '0 1 0 0',
			selModel:sm,
			store: store,
			border: false,
			tbar:[{
				xtype: 'panel',
				border: false,
				height: 25,
				width: '100%',
				bodyPadding: 0-1,
				layout: 'column',
				defaults: {columnWidth: 1,bodyPadding: 0},
				items: [{
						xtype: 'toolbar',
						items: [{
							xtype:"textfield",
							fieldLabel : '标题',	
							labelWidth:40,
							maxLength:20,
							width:160
						},{
							xtype:"textfield",
							fieldLabel : '标签',	
							labelWidth:40,
							maxLength:20,
							width:160
						},{
							xtype:"textfield",
							fieldLabel : '日志内容',	
							labelWidth:60,
							maxLength:150,
							width:180
						},{
							x:250,
							y:65,
							name: 'startTime',
							fieldLabel: '开始时间',
							labelWidth:60,
							width:180,
							xtype:'datefield',
							format:'Y-m-d',
							editable:false
						},{
							x:520,
							y:65,
							name: 'endTime',
							fieldLabel: '结束时间',
							labelWidth:60,
							width:180,
							xtype:'datefield',
							format:'Y-m-d',
							editable:false
						},{
							text:"查询",
							iconCls: 'icon-search',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								if(items[3].getSubmitValue()>items[4].getSubmitValue() && items[4].getSubmitValue() !=''){
									Ext.Msg.alert('提示', '开始时间不能大于结束时间！');
									return;
								}
								logSysPanel.store.proxy.extraParams = {
									title: items[0].getSubmitValue(),
									mark: items[1].getSubmitValue(),
									content: items[2].getSubmitValue(),
									startTime: items[3].getSubmitValue(),
									endTime: items[4].getSubmitValue()
								};
								logSysPanel.store.loadPage(1);
							}
						},{
							text:"重置",
							iconCls: 'icon-b_reset',
							handler: function(){
								var tbar = this.up('toolbar');
								var items = tbar.items.items;
								items[0].setValue();
								items[1].setValue();
								items[2].setValue();
								items[3].setValue();
								items[4].setValue();
							}
						},{
							text: '删除信息',
							iconCls: 'icon-delete',
							handler: function(){
								var rec = logSysPanel.getSelectionModel().getSelection();
								if(rec.length > 0){
									me.delUserLogs(rec);
								}
								else{
									Ext.Msg.alert('提示', '请选择要删除的信息！');
								}
							}
						}
					]}
				 ]
			}],
			bbar: Ext.create('Ext.PagingToolbar', {
	            store: store,
	            id: me.id+'-bbar',
	            displayInfo: true,
	            displayMsg: '显示第 {0} 至 {1} 条记录，共 {2} 条',
	            emptyMsg: "无记录"
	        }),
			columns: [{
	            text: '标题',
	            flex: 1,
	            dataIndex: 'title'
			},{
	            text: '标签',
	            flex: 1,
	            dataIndex: 'mark'
			},{
	            text: '类型',
	            flex: 1,
	            dataIndex: 'type',
	            renderer: function(v){
		    		return v == 1 ? "业务":"系统";
		    	}
			},{
	            text: '日志内容',
	            flex: 3,
	            dataIndex: 'content'
			},{
	            text: '操作ip',
	            flex: 1,
	            dataIndex: 'ipaddr'
			},{
	            text: '状态',
	            flex: 1,
	            dataIndex: 'success',
	            renderer: function(v){
		    		return v == 1 ? "<font color='green'>成功</font>":"<font color='red'>失败</font>";
		    	}
			},{
	            text: '操作时间',
	            flex: 1,
	            dataIndex: 'crtime',
	            renderer: formatDate
			},{
	            text: '操作用户',
	            flex: 1,
	            dataIndex: 'name'
			}]
		});
		
		
		me.items = [logSysPanel];
		
		me.callParent();
	},
	delUserLogs: function(rec){
		var me = this;
		if(rec){
			Ext.Msg.confirm('提示', '你确定要删除当前信息吗？',function(b){
				if('yes' == b){
					var ids=[];
					var para = {};
					for ( var i = 0; i < rec.length; i++) {
						ids.push(rec[i].get('id'));
						para.content = rec[i].get('content');
						para.title = rec[i].get('title');
						para.crtime = rec[i].get('crtime');
						para.name = rec[i].get('name');
					}
					Ext.Ajax.request({
					    url: 'logAction/delUserLogs.do',
					    params: {ids : ids,para:para},
					    success: function(response){
        	    	    	var rs = Ext.decode(response.responseText);
        	    	    	if(rs.rst){
        	    	    		Ext.Msg.alert('提示', '删除成功！');
        	    	    		me.logSysPanel.store.reload();
        	    	    	}
        	    	    	else{
        	    	    		Ext.Msg.alert('提示', '删除失败!');
        	    	    	}
					    }
					});
				}
			});
		}
		else{
			Ext.Msg.alert('提示', '请选择要删除的信息！');
		}
	}
});